/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.reportes;
import Model.LogUsuario;
import Model.Usuario;
import Presenter.LogPresenter;
import Principals.Validadores;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author Paola
 */
public class Frm_ReporteSeguridad extends javax.swing.JDesktopPane {

    /**
     * Creates new form Frm_ReporteSeguridad
     */
     private DefaultTableModel mdlBuscarVehiculo;
private List<LogUsuario> listaLog;   
   
    public Frm_ReporteSeguridad() {
        initComponents();
          mdlBuscarVehiculo = new DefaultTableModel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEnvios = new javax.swing.JTable();
        pnlBuscarPedido3 = new javax.swing.JPanel();
        lblIntervaloFec3 = new javax.swing.JLabel();
        lblDesde3 = new javax.swing.JLabel();
        lblHasta3 = new javax.swing.JLabel();
        jdcDesde = new com.toedter.calendar.JDateChooser();
        jdcHasta = new com.toedter.calendar.JDateChooser();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 895, Short.MAX_VALUE)
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 423, Short.MAX_VALUE)
        );

        tblEnvios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id log", "Accion", "Perfil", "Usuario", "Fecha", "Tabla", "Ip"
            }
        ));
        jScrollPane1.setViewportView(tblEnvios);

        pnlBuscarPedido3.setBorder(javax.swing.BorderFactory.createTitledBorder("Parámetros"));

        lblIntervaloFec3.setText("Intervalo de Fechas");

        lblDesde3.setText("Desde:");

        lblHasta3.setText("Hasta:");

        jButton4.setText("Buscar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/pdf.jpg"))); // NOI18N
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton1.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton1.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton1.setPreferredSize(new java.awt.Dimension(295, 241));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarPedido3Layout = new javax.swing.GroupLayout(pnlBuscarPedido3);
        pnlBuscarPedido3.setLayout(pnlBuscarPedido3Layout);
        pnlBuscarPedido3Layout.setHorizontalGroup(
            pnlBuscarPedido3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPedido3Layout.createSequentialGroup()
                .addGroup(pnlBuscarPedido3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblIntervaloFec3)
                    .addGroup(pnlBuscarPedido3Layout.createSequentialGroup()
                        .addComponent(lblDesde3)
                        .addGap(32, 32, 32)
                        .addComponent(jdcDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(lblHasta3)
                        .addGap(18, 18, 18)
                        .addComponent(jdcHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(39, 39, 39)
                .addComponent(jButton4)
                .addGap(39, 39, 39)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(259, Short.MAX_VALUE))
        );
        pnlBuscarPedido3Layout.setVerticalGroup(
            pnlBuscarPedido3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBuscarPedido3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblIntervaloFec3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlBuscarPedido3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jdcDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jdcHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDesde3)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4)
                    .addComponent(lblHasta3))
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Reporte de log del sistema");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1)
                    .addComponent(pnlBuscarPedido3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(33, 33, 33))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlBuscarPedido3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        Date fDesde = jdcDesde.getDate();
        Date fHasta = jdcHasta.getDate();
        mdlBuscarVehiculo.setNumRows(0);
        
        if(fDesde==null||fHasta==null){
            JOptionPane.showMessageDialog(this, "Ingrese ambas fechas");
            return ;
        }

        if (!fDesde.before(fHasta)){
            JOptionPane.showMessageDialog(this, "Corriga las fechas ingresadas");
            return ;

        }
        mdlBuscarVehiculo.setNumRows(0);
        mdlBuscarVehiculo = (DefaultTableModel) tblEnvios.getModel();

        /*Object[] row = new Object[]{"4","Crear","Administrador","Admin","12/12/12 5:04 p.m.","Vehiculo","172.162.13.12"};
        mdlBuscarVehiculo.insertRow(0, row);

         row = new Object[]{"5","Modificar","Administrador","Admin","12/12/12 5:07 p.m.","Vehiculo","172.162.13.12"};
        mdlBuscarVehiculo.insertRow(0, row);
*/
        
        
                
        
listaLog= LogPresenter.buscarLog(fDesde, fHasta, null);
        
 Object[] row ;
        for (int i = 0; i < listaLog.size(); i++) {
            LogUsuario v = listaLog.get(i);
                 
            
                row = new Object[]{v.getIdLog(),v.getAccion(),v.getPerfil(),v.getUsuario(),v.getFecha(),v.getTabla(),v.getIp()};
            mdlBuscarVehiculo.insertRow(i, row);
        }
        

        
        
        
        
        
                 /*LinkedList<LogUsuario> listaLogs = new LinkedList<LogUsuario>(); 

                 LogUsuario lg = new LogUsuario();
                 lg.setAccion("lg.accion");
                         lg.setIp("ip");
                 listaLogs.add(lg); */
//listaLogs.add(new LogUsuario("Marcelo C", "500")); 
//listaLogs.add(new LogUsuario("Don Bill", "5000")); 
        /*if (listaLog.size()==0)return;
        
try 
{ 
    JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("src/Reportes/ReporteLog.jasper"); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(listaLog)); 
    JRExporter exporter = new JRPdfExporter(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("LogDeUsuario.pdf")); 
    exporter.exportReport(); 
    if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("reporteEnPdf2.pdf");
        Desktop.getDesktop().open(myFile);
    } catch (Exception ex) {
        // no application registered for PDFs
    }
}
} 
catch(JRException e) 
{ 
    e.printStackTrace(); 
} 

        */
        
        
        
        
        
        
        
        
        
        
        
        
        /*
        if (txtDNI.getText().equals("88888888")){
            encontrado=true;
            mdlBuscarVehiculo.setNumRows(0);
            mdlBuscarVehiculo = (DefaultTableModel) tblEnvios.getModel();

            Object[] row = new Object[]{"1233","Jose Ruiz","Celular","12/12/12 3:00 pm","12/12/12 5:00 pm","Entregado"};
            mdlBuscarVehiculo.insertRow(0, row);

            row = new Object[]{"1425","Jose Ruiz","Celular","29/12/12 1:00 pm","","Pendiente"};
            mdlBuscarVehiculo.insertRow(0, row);
        }
        */

        //limpiar();
        //   JOptionPane.showMessageDialog(this, "Se encontró " + mdlBuscarVehiculo.getRowCount() + " conincidencia(s)");
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        
        
         
        
try 
{ 
    if(mdlBuscarVehiculo.getRowCount()==0)return;
    if (listaLog.size()==0)return;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHHmmss");
Date date = new Date();
String time = sdf.format(date);
    JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("src/Reportes/ReporteLog.jasper"); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(listaLog)); 
    JRExporter exporter = new JRPdfExporter(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("LogDeUsuario_"+ time  + ".pdf")); 
    exporter.exportReport(); 
    if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("LogDeUsuario_"+ time  + ".pdf");
        Desktop.getDesktop().open(myFile);
    } catch (Exception ex) {
        // no application registered for PDFs
    }
}
} 
catch(JRException e) 
{ 
    e.printStackTrace(); 
} 
        
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
              
try 
{ 
    if(mdlBuscarVehiculo.getRowCount()==0)return;
    if (listaLog.size()==0)return;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHHmmss");
Date date = new Date();
String time = sdf.format(date);
    JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("src/Reportes/ReporteLog.jasper"); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(listaLog)); 
    JRExporter exporter = new JRPdfExporter(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("LogDeUsuario_"+ time  + ".pdf")); 
    exporter.exportReport(); 
    if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("LogDeUsuario_"+ time  + ".pdf");
        Desktop.getDesktop().open(myFile);
    } catch (Exception ex) {
        // no application registered for PDFs
    }
}
} 
catch(JRException e) 
{ 
    e.printStackTrace(); 
} 
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private com.toedter.calendar.JDateChooser jdcDesde;
    private com.toedter.calendar.JDateChooser jdcHasta;
    private javax.swing.JLabel lblDesde3;
    private javax.swing.JLabel lblHasta3;
    private javax.swing.JLabel lblIntervaloFec3;
    private javax.swing.JPanel pnlBuscarPedido3;
    private javax.swing.JTable tblEnvios;
    // End of variables declaration//GEN-END:variables
}
